//
//  WmcCloudClassifyAlbumCell.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/14.
//

import UIKit

class WmcCloudClassifyAlbumCell: WmcBaseCollectionCell {
    
    override func makeUI() {
        titleLabel.backgroundColor = .clear
        titleLabel.textColor = .wmc_grayH2_323233
        titleLabel.font = 15.wmc_font
        
        imageView.contentMode = .scaleAspectFill
        imageView.backgroundColor = .wmc_grayH11_9D9DB3
        imageView.clipsToBounds = true
        
        contentView.addSubview(imageView)
        contentView.addSubview(titleLabel)
        contentView.addSubview(descLabel)
        
        imageView.snp.makeConstraints { make in
            make.left.right.top.equalToSuperview()
            make.bottom.equalTo(-50)
        }
        
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(8)
            make.left.right.equalToSuperview()
            make.height.equalTo(21)
        }
        
        descLabel.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(titleLabel.snp.bottom)
            make.height.equalTo(21)
        }
    }
    
    private lazy var descLabel: UILabel = {
        let label = UILabel()
        label.textColor = .wmc_grayH5_606066
        label.font = 14.wmc_font
        return label
    }()
    
    func bindModel(_ model: WmcMediaAlbumModel) {
        var params = ""
        
        if model.resourceType == 0 {
            params = "x-oss-process=image/resize,h_200,m_lfit"
        } else if model.resourceType == 1 {
            params = "x-oss-process=video/snapshot,t_7000,ar_auto,f_jpg,m_fast"
        }
        
        var url = "\(model.url)"
        var symbol = "?"
        if url.contains(symbol) {
            symbol = "&"
        }
        url.append(contentsOf: "\(symbol)\(params)")
        
        if let imgUrl = URL(string: url) {
            let urlParts = model.url.components(separatedBy: "?")
            var key = model.url
            if urlParts.count == 2 {
                key = urlParts[0]
            }
            key.append("/thumb")
            imageView.kf.setImage(with: .network(Kingfisher.ImageResource(downloadURL: imgUrl, cacheKey: key)), placeholder: "image_placeholder".wmc_image)
        }
        
        titleLabel.text = model.title
        if model.typeEnum() == .watermarkPhoto {
            descLabel.text = "\(model.num)个"
        } else if model.typeEnum() == .teamMember {
            descLabel.text = "\(model.num)人"
        } else {
            descLabel.text = "\(model.num)张"
        }
    }
}
